import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.netflix.client.ClientFactory;
import com.netflix.client.http.HttpRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.config.ConfigurationManager;
import com.netflix.niws.client.http.RestClient;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;

import java.io.InputStream;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

public class RibbonClientPostParamDemo {

    public static void main(String[] args) throws Exception {
        ConfigurationManager.loadPropertiesFromResources("client.properties");
        System.out.println(ConfigurationManager.getConfigInstance().getProperty("sample-client.ribbon.listOfServers"));
        RestClient client = (RestClient) ClientFactory.getNamedClient("sample-client");
        try {
            final ObjectMapper mapper = new ObjectMapper();
            Map<String,Object> params = new HashMap<>();
            params.put("age","100");
            params.put("name","abc");
            params.put("date","1");

            HttpRequest request = HttpRequest.newBuilder()
                    .verb(HttpRequest.Verb.POST)
                    .uri(new URI("/format/date2"))
                    .entity(mapper.writeValueAsString(params))
                    .header("Content-Type", "application/json;charset=utf8")
                    .build();
            for (int i = 0; i < 20000; i++) {
                HttpResponse response = client.executeWithLoadBalancer(request);
                System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
                InputStream inputStream = response.getInputStream();
                int size = inputStream.available();
                byte[] bytes = new byte[size];
                inputStream.read(bytes);
                String result = new String(bytes,"utf-8");
                System.out.println(result);
                inputStream.close();
                Thread.currentThread().sleep(888);
            }
        }catch (JsonProcessingException e) {
            e.printStackTrace();
        }finally {
            client.shutdown();
        }


       /* ZoneAwareLoadBalancer lb = (ZoneAwareLoadBalancer) client.getLoadBalancer();
        System.out.println(lb.getLoadBalancerStats());
        ConfigurationManager.getConfigInstance().setProperty("sample-client.ribbon.listOfServers", "www.baidu.com:80,www.linkedin.com:80");
        System.out.println("changing servers ...");
        Thread.sleep(3000);
        for (int i = 0; i < 20; i++) {
            HttpResponse response = client.executeWithLoadBalancer(request);
            System.out.println("Status for URI:" + response.getRequestedURI() + " is :" + response.getStatus());
        }
        System.out.println(lb.getLoadBalancerStats());*/
    }
}
